﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DevDemo
{
    public partial class ListBoxBtn : UserControl
    {
        public ListBoxBtn()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ListBoxBtn_Load(object sender, EventArgs e)
        {   //去除首列
            dataGridView1.RowHeadersVisible = false;

            string[] strArr = { "1", "2", "3", "4", "5" };

            DataGridViewCellStyle columnHeaderStyle = new DataGridViewCellStyle();

            columnHeaderStyle.BackColor = Color.Beige;

            columnHeaderStyle.Font = new Font("Verdana", 10, FontStyle.Bold);

            dataGridView1.ColumnHeadersDefaultCellStyle = columnHeaderStyle;

            this.dataGridView1.Columns.Add("1", "bb");

            //this.dataGridView1.Columns.Add("2", "qq");

            //this.dataGridView1.Columns.Add("3", "dd");

            this.dataGridView1.Columns.Add("4", "aa");

        }

        private void button1_Click(object sender, EventArgs e)
        {
            DataGridViewRow dr = new DataGridViewRow();

            foreach (DataGridViewColumn c in this.dataGridView1.Columns)
            {
                dr.Cells.Add(c.CellTemplate.Clone() as DataGridViewCell);  //给行添加单元格
            }

            dr.Cells[0].Value = "1111";

            //dr.Cells[2].Value = "3333";

            this.dataGridView1.Rows.Add(dr);

            int index = this.dataGridView1.Rows.Count - 2;

            //ComboBox com = new ComboBox();

            //com.Name = "Containers" + index.ToString(); ;

            //com.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;

            //com.FlatStyle = System.Windows.Forms.FlatStyle.Flat;

            //com.Items.AddRange(new object[] {

            //"1",

            //"2",

            //"3",

            //"4"});

            //com.SelectedIndex = 0;

            //this.dataGridView1.Controls.Add(com);

            //this.dataGridView1.Columns[1].Width = com.Width;

            //com.Location = new System.Drawing.Point(((this.dataGridView1.GetCellDisplayRectangle(1, index, true).Right) - (com.Width)), this.dataGridView1.GetCellDisplayRectangle(1, index, true).Y);


            //Button btn1 = new Button();

            //btn1.Name = "btnRun" + index.ToString(); ;

            //btn1.Text = "Run";

            //btn1.Click += new EventHandler(btn1_Click);


            Button btn2 = new Button();

            btn2.Name = "btnRemove" + index.ToString();

            btn2.Text = "Remove";

            btn2.Click += new EventHandler(btn2_Click);

            //this.dataGridView1.Controls.Add(btn1);

            this.dataGridView1.Controls.Add(btn2);

            this.dataGridView1.Columns[1].Width = btn2.Width + 6;//+ btn2.Width

            //btn1.Location = new System.Drawing.Point(((this.dataGridView1.GetCellDisplayRectangle(3, index, true).Left)), this.dataGridView1.GetCellDisplayRectangle(3, index, true).Y);

            btn2.Location = new System.Drawing.Point(((this.dataGridView1.GetCellDisplayRectangle(1, index, true).Right - 1) - (btn2.Width)), this.dataGridView1.GetCellDisplayRectangle(1, index, true).Y);
        }

        public void btn1_Click(object sender, EventArgs e)
        {
            //this.richTextBox1.Text = "";

            //Button btn = (Button)(sender);

            ////这个btn的name是btnRun打头的

            //string suffix = btn.Name.ToString().Substring(6); //后边那个号，相当于index的string

            //Control c = findControlByName(suffix);

            //if (c != null)
            //{
            //    ComboBox com = (ComboBox)(c);

            //    //Control ctl1 = this.dataGridView1.Controls["Containers" + i.ToString()];

            //    //ComboBox com = (ComboBox)ctl1;　　其实这样写更简单点

            //    for (int i = 0; i < com.Items.Count; i++)
            //    {

            //        this.richTextBox1.Text += com.Items[i].ToString() + "\n";
            //    }
            //}

        }

        public void btn2_Click(object sender, EventArgs e)
        {
            int RowCount = this.dataGridView1.Rows.Count;

            Button btn = (Button)(sender);

            //这个btn的name是btnRemove打头的

            string suffix = btn.Name.ToString().Substring(9);  //后边那个号，相当于index的string

            this.dataGridView1.Controls.RemoveByKey(btn.Name);

            this.dataGridView1.Controls.RemoveByKey("btnRun" + suffix);

            this.dataGridView1.Controls.RemoveByKey("Containers" + suffix);

            int index = Convert.ToInt32(suffix);

            this.dataGridView1.Rows.RemoveAt(index);

            if (index < RowCount - 2)
            {
                for (int i = index + 1; i < RowCount - 1; i++)
                {
                    //Control ctl1 = this.dataGridView1.Controls["Containers" + i.ToString()];

                    //Control ctl2 = this.dataGridView1.Controls["btnRun" + i.ToString()];

                    Control ctl3 = this.dataGridView1.Controls["btnRemove" + i.ToString()];

                    //ComboBox com = (ComboBox)ctl1;

                    //Button btnRun = (Button)ctl2;

                    Button btnRemove = (Button)ctl3;

                    //上移一格，单元格Height位4，Button的Height为23

                    //com.Location = new System.Drawing.Point(com.Location.X, com.Location.Y - 23);

                    //btnRun.Location = new System.Drawing.Point(btnRun.Location.X, btnRun.Location.Y - 23);

                    btnRemove.Location = new System.Drawing.Point(btnRemove.Location.X, btnRemove.Location.Y - 23);

                    //改名字的后缀

                    int j = i - 1;

                    //com.Name = "Containers" + j.ToString();

                    //btnRun.Name = "btnRun" + j.ToString();

                    btnRemove.Name = "btnRemove" + j.ToString();

                }

            }

        }

        public Control findControlByName(string suffix)
        {
            foreach (System.Windows.Forms.Control c in this.dataGridView1.Controls)
            {
                if (c.Name == "Containers" + suffix)

                    return c;
            }

            return null;
        }

    }
}
